<template>
  <view>
     <view class="login-container">
        <!-- 提示登录的图标 -->
        <uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
        <!-- 登录按钮 -->
        <button type="primary" class="btn-login" @click="getUserInfo">一键登录</button>
        <!-- 登录提示 -->
        <view class="tips-text">登录后尽享更多权益</view>
      </view>
  </view>
</template>

<script>
  import {mapState,mapGetters,mapMutations} from "vuex"
  export default {
    name:"my-login",
    data() {
      return {
        
      };
    },
    computed:{
      ...mapState("m_user",['token']),
    },
    methods:{
      ...mapMutations("m_user",['updateUserInfo','updateToken']),
      async getUserInfo(){
        const [err,res] = await uni.getUserProfile({
          desc:"获取用户信息"
        }).catch(err => err)
        if(err?.errMsg == "getUserProfile:fail auth deny") return uni.$showMsg("取消授权！")
        console.log(res)
        // 持久化保存用户信息
        this.updateUserInfo(res.userInfo)
        // 根据res用户信息来获取token
        this.getToken(res)
      },
      async getToken(res){
        // 因为后台获取token的方法有问题,这里用一个固定的token来代替
         const token = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo"
         this.updateToken(token)
      }
    },
  }
</script>

<style lang="scss">
.login-container{
  height: 750rpx;
  background-color: #efefef;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .uni-icons{
    
  }
  .btn-login{
    border-radius: 100px;
    background-color: #f00;
    width: 90%;
  }
  .tips-text{
    font-size: 12px;
    color: gray;
    margin-top: 10px;
  }
}
</style>